/**
 * 
 */
package cn.alexy.dbbench.server;

import cn.alexy.dbbench.server.service.DBBenchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

//import cn.alexy.dbbench.server.repository.UserDao;
//import cn.alexy.dbbench.server.service.account.AccountService;
//import cn.alexy.dbbench.server.web.account.UserAdminController;




/**
 * @author Administrator付a
 *
 */
@Component
public class TempRun {
	
//	private UserDao userDao;
	private DBBenchService dbbs;
	private static Logger logger = LoggerFactory.getLogger(TempRun.class);
	private SimpleDriverDataSource dataSource;
	private static BeanFactory beanFactory;
	public static LocalContainerEntityManagerFactoryBean entityManagerFactory;
	
	
	private static final String IDS = "mysql";

	
	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		System.setProperty("spring.profiles.active", IDS);
		
//		net.sf.log4jdbc.


		//临时测试
//		logger.warn("DBBench!");
//		
//        String commandStr = "sc";
//        RunCommand.exeCmd(commandStr);
        
		
		beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
		TempRun tempRun = beanFactory.getBean("tempRun",TempRun.class);
		System.out.println(System.getProperty("user.dir"));
		HibernateJpaVendorAdapter hjva = beanFactory.getBean("hibernateJpaVendorAdapter",HibernateJpaVendorAdapter.class);
		logger.info(hjva.getJpaDialect().toString());
		logger.info(hjva.getJpaPropertyMap().toString());
		
		tempRun.test();

	}
	
	@Autowired
	public void setDBBenchService(DBBenchService dbbs) {
		this.dbbs = dbbs;
	}	
	
	@Autowired
	public void setDataSource(SimpleDriverDataSource ds) {
		this.dataSource = ds;
	}
	
//	@Autowired
//	public void setEntityManagerFactory(LocalContainerEntityManagerFactoryBean em) {
//		this.entityManagerFactory = em;
//	}	
	

//	@Autowired
//	public void setUserDao(UserDao userDao) {
//		this.userDao = userDao;
//	}
	
	public void test() throws SQLException{
//		System.out.println(dbbs.getCount());
//		dbbs.clsData();
		Connection cn = dataSource.getConnection();
		DatabaseMetaData dbm = cn.getMetaData();
//		dbbs.getResult().setComm(dbm.getDatabaseProductName()+" "+dbm.getDatabaseMajorVersion()+"."+dbm.getDatabaseMinorVersion());
		dbbs.getResult().setComm(dbm.getDatabaseProductName()+" "+dbm.getDatabaseProductVersion());
		dbbs.getResult().setDriver(dbm.getDriverName()+" "+dbm.getDriverMajorVersion()+"."+dbm.getDriverMinorVersion());
		dbbs.getResult().setAdapter("JPA Hibernate");
		HibernateJpaVendorAdapter hjva = beanFactory.getBean("hibernateJpaVendorAdapter",HibernateJpaVendorAdapter.class);
		String vendor = hjva.getJpaPropertyMap().toString();
		dbbs.getResult().setVendor(vendor.substring(vendor.lastIndexOf('.')+1,vendor.lastIndexOf('}')));
		
		cn.close();
//		org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean em = null;
//		em.nativeEntityManagerFactory.isOpen();

		System.out.println(dbbs.getResult().toString());
		dbbs.getResult().setSumtime(0L);
		dbbs.fillData(dbbs.loadtoMem(dbbs.getResult().getDatacount()));		
		dbbs.sumData();
		dbbs.editData();
		dbbs.sumData();
		dbbs.queryData();
//		dbbs.delData();
		logger.info(dbbs.getResult().toString());
		dbbs.getResult().fillmark();
//		dbbs.saveResultByRest();
//        dbbs.saveResultToNoSQL();
//		dbbs.saveResult();
	}

}
